import json
import cv2
from loguru import logger
from typing import Type

from app.core.conf import ST


from cv.image.BaseImg import BaseImg
from cv.filter.filter import Filter
from cv.classify.classify_state import ClassifyState
from cv.classify.classify_th import ClassifyTH
from rasip.camera import get_rasip_photo
from sio.agv import send_cvinfo
from app.api.service import img_service
from app.api.service import qm_service
from tornado.dev import tornadoDev

async def handle_red_temp_blue_humi(station,dev,timestamp):
    # 先处理
    short_cfy = handle_short_camera(dev,station,timestamp)

    if not short_cfy.is_run_state():
        BaseImg.save_draw(timestamp,short_cfy.img_draw)
        logger.debug('当前机台非运行状态,暂停继续往下识别')
        # 发送socketio消息到前端，存sqlite数据库，存oracle数据库,发龙卷风
        await send_cvinfo(timestamp,station,dev,short_cfy)
        await img_service.storage_cv_info(timestamp,station,dev,short_cfy)
        # 存储到oracle数据库
        qm_service.create_qm_data(timestamp,station,dev,short_cfy)
        tornadoDev.send_dev_info(timestamp,station,dev,short_cfy)
        return False


    logger.debug('当前机台运行状态，继续识别长镜头')

    # 先处理
    long_cfy = handle_long_camera(dev,station,timestamp)
    
    if not long_cfy.classify_flag:
        logger.debug('长镜头没有识别好，需要重新识别')
        return True

    BaseImg.save_draw(timestamp,long_cfy.img_draw)
    await send_cvinfo(timestamp,station,dev,short_cfy,long_cfy)
    tornadoDev.send_dev_info(timestamp,station,dev,short_cfy,long_cfy)
    await img_service.storage_cv_info(timestamp,station,dev,short_cfy,long_cfy)
    qm_service.create_qm_data(timestamp,station,dev,short_cfy,long_cfy)

    return False
    


 

def handle_short_camera(dev,station,timestamp)->Type[ClassifyState]:
    img = get_rasip_photo(ST.SHORT_CAMERA_URL)
    baseimg = BaseImg(dev=dev,station=station,img_raw=img,timestamp=timestamp)
    baseimg.ocr_img()
    # baseimg.log_ocr()
    filter = Filter(baseimg.ocr_raw_list,dev)
    filter.filter_pure_chinese()
    # filter.log_filter_ocr_yellow()
    baseimg.highlight_ocr(filter.ocr_filter_list)
    cfy = ClassifyState(ocr_raw_list=filter.ocr_filter_list)
    cfy.classify()
    baseimg.draw_ocr(cfy.ocr_filter_list)
    cfy.img_draw = baseimg.img_draw
    return cfy




def handle_long_camera(dev,station,timestamp)->Type[ClassifyState]:
    img = get_rasip_photo(ST.LONG_CAMERA_URL)
    baseimg = BaseImg(dev=dev,station=station,img_raw=img,timestamp=timestamp)
    # 因为长镜头拍摄的图片，所以要缩放之后在进行ocr识别，否则字符会断开
    baseimg.ocr_img()
    # baseimg.ocr_scale()
    # baseimg.highlight_ocr()
    # baseimg.log_ocr()
    filter = Filter(baseimg.ocr_raw_list,dev)
    filter.filter_temp_humi()
    filter.filter_base()
    # filter.log_filter_ocr_yellow(filter.ocr_temp_humi_list)
    # baseimg.highlight_ocr(filter.ocr_temp_humi_list)
    cfy = ClassifyTH(ocr_raw_list=filter.ocr_temp_humi_list,ocr_base_list=filter.ocr_base_list)
    cfy.classify()
    # logger.debug(cfy.ocr_raw_list);
    baseimg.draw_ocr(cfy.ocr_raw_list)
    cfy.img_draw = baseimg.img_draw
    return cfy

